package service;

import dao.CardDaoMapper;
import entity.Card;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import utils.MyBatisUtil;

import java.util.ArrayList;
import java.util.List;

public class CardService {
    public List<Card> list(@Param("pageIndex") int pageIndex,@Param("pageSize") int pageSize){
        List<Card> list = new ArrayList<Card>();
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            list = sqlSession.getMapper(CardDaoMapper.class).list(pageIndex,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }
    //删除数据
    public int delete(@Param("id") Integer id){
        int result = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            result = sqlSession.getMapper(CardDaoMapper.class).delete(id);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }
    //增加数据
    public int add(Card card){
        int result = 0;
        SqlSession sqlSession = null;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            result = sqlSession.getMapper(CardDaoMapper.class).add(card);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }

    //修改
    public int update(@Param("id") Integer id,@Param("status") Integer status){
        int result = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            result = sqlSession.getMapper(CardDaoMapper.class).update(id, status);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }
    //根据身份证号查询单个信息
    public Card select(@Param("idNumber") String idNumber){
        Card card = new Card();
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            card = sqlSession.getMapper(CardDaoMapper.class).select(idNumber);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return card;
    }
    //模糊查询
    public List<Card> selectlist(@Param("userName") String userName,@Param("status") Integer status){
        List<Card> list = new ArrayList<Card>();
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            list = sqlSession.getMapper(CardDaoMapper.class).selectlist(userName,status);
        }catch (Exception e){

        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }
}
